package com.ymatou.apigateway.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.apigateway.parameter.AdjustSkuStock;
import com.ymatou.apigateway.parameter.AesUtil;
import com.ymatou.apigateway.parameter.OpenapiReq;
import com.ymatou.apigateway.parameter.OpenAdjustStockBean;
import com.ymatou.apigateway.service.OpenapiService;
import com.ymatou.apigateway.service.OpenAdjustStockCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.DetailsCatalogDto;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.shop2cn.iapi.agentproducts.parameter.UpdateStockDto;
import com.ymatou.iapi.godmch.parameter.createDepotBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.database.model.Application;
import com.ymttest.database.model.AuthCode;
import com.ymttest.database.model.godmch.mch_depot_info;
import com.ymttest.database.sqlwapper.OpenApiWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
/**
*更新商品基础信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_OpenAdjustStock{
	private static OpenapiReq openapiReq;
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
	private static IntergratedProductMapper ipm = SqlMapperFactory.getIntergratedProductMapper();
	private static OpenAdjustStockBean OpenAdjustStockBean;
	private static OpenAdjustStockCall OpenAdjustStockCall;
	private static int RootUserId = Integer.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新商品基础信息");
	}

	@Before
	public void caseUp() {
		OpenAdjustStockBean=new OpenAdjustStockBean();
		OpenAdjustStockCall=new OpenAdjustStockCall();
		openapiReq = new OpenapiReq();
		openapiReq.setMethod("sq.sku.stock.adjust");
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(OpenAdjustStockCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	//com.shop2cn.iapi.agentproducts.testcase.Ts_UpdateProductBase
	public void Tc_OpenAdjustStock_001() {
		Logger.start(true,"更新商品库存信息");
		try {
			//前置数据
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(RootUserId,
                    "testname", false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
            ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(RootUserId, productId);
            String catalogId = productDetailsDto.getCatalogList().get(0).getCatalogId();
            DetailsCatalogDto ca1 = productDetailsDto.getCatalogList().stream().filter(x->x.getCatalogId().equals(catalogId)).findFirst().get();
			int real=TestDataManager.getRandomIntNum(2)+1;
			int virtual=TestDataManager.getRandomIntNum(2)+1;
			
			AddAgentProductBean addAgentProductBean2 = AddAgentProductCallService.initAddAgentProduct(RootUserId,
                    "testname", false);
            String productId2 = AddAgentProductCallService.AddAgentProduct(addAgentProductBean2);
            ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(RootUserId, productId2);
            String catalogId2 = productDetailsDto2.getCatalogList().get(0).getCatalogId();
            DetailsCatalogDto ca2 = productDetailsDto2.getCatalogList().stream().filter(x->x.getCatalogId().equals(catalogId2)).findFirst().get();
			int real2=TestDataManager.getRandomIntNum(2)+1;
			int virtual2=TestDataManager.getRandomIntNum(2)+1;
			
//            OpenAdjustStockBean.setUserId(RootUserId);
			String bizno=YMTDateUtil.getBeforeOrNextDay(0, "yyyyMMddHHmmss")+TestDataManager.getRandomNum(4);
            OpenAdjustStockBean.setBizNo(bizno);
            List<AdjustSkuStock> astocks=new ArrayList<AdjustSkuStock>();
            AdjustSkuStock adjustSkuStock1=new AdjustSkuStock();
//            adjustSkuStock1.setProduct_id(productId);
            adjustSkuStock1.setSku_id(catalogId);
            adjustSkuStock1.setStock_num(real);
            adjustSkuStock1.setVirtual_stock_num(virtual);
			astocks.add(adjustSkuStock1);
			
			 AdjustSkuStock adjustSkuStock2=new AdjustSkuStock();
//			 adjustSkuStock2.setProduct_id(productId);
			 adjustSkuStock2.setSku_id(catalogId2);
			 adjustSkuStock2.setStock_num(real2);
			 adjustSkuStock2.setVirtual_stock_num(virtual2);
			astocks.add(adjustSkuStock2);
				
			OpenAdjustStockBean.setSku_stocks(astocks);
			
			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper
					.selectApplicationBySellerId((long) RootUserId).get(0);
			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();
			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);
			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(OpenAdjustStockBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.sku.stock.adjust",
							app_id);
			OpenAdjustStockCall.setUrl(url);
			OpenAdjustStockCall.setData(openapiReq);
			OpenAdjustStockCall.callService();			
			Logger.verifyEquals("0000", OpenAdjustStockCall.getString("code"), "验证返回");
			Thread.sleep(5000);
			ProductDetailsDto productDetailsDto1 = AddAgentProductCallService.selectProductDetails(RootUserId, productId);
			DetailsCatalogDto rca1 = productDetailsDto1.getCatalogList().stream().filter(x->x.getCatalogId().equals(catalogId)).findFirst().get();
			Logger.verifyEquals(ca1.getRealStock()+real,rca1.getRealStock(),"验证real");
			Logger.verifyEquals(ca1.getVirtualStock()+virtual,rca1.getVirtualStock(),"验证virtual");
			Logger.verifyEquals(ca1.getStock()+real+virtual,rca1.getStock(),"验证stock");
			
			ProductDetailsDto rproductDetailsDto2 = AddAgentProductCallService.selectProductDetails(RootUserId, productId2);
			DetailsCatalogDto rca2 = rproductDetailsDto2.getCatalogList().stream().filter(x->x.getCatalogId().equals(catalogId2)).findFirst().get();
			Logger.verifyEquals(ca2.getRealStock()+real2,rca2.getRealStock(),"验证real");
			Logger.verifyEquals(ca2.getVirtualStock()+virtual2,rca2.getVirtualStock(),"验证virtual");
			Logger.verifyEquals(ca2.getStock()+real2+virtual2,rca2.getStock(),"验证stock");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	//com.shop2cn.iapi.agentproducts.testcase.Ts_UpdateProductBase
	public void Tc_OpenAdjustStock_002() {
		Logger.start(true,"更新商品库存信息-扣减");
		try {
			//前置数据
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(RootUserId,
                    "testname", false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
            ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(RootUserId, productId);
            String catalogId = productDetailsDto.getCatalogList().get(0).getCatalogId();
            DetailsCatalogDto ca1 = productDetailsDto.getCatalogList().stream().filter(x->x.getCatalogId().equals(catalogId)).findFirst().get();
			int real=-TestDataManager.getRandomIntNum(2)+1;
			int virtual=TestDataManager.getRandomIntNum(2)+1;
			
			AddAgentProductBean addAgentProductBean2 = AddAgentProductCallService.initAddAgentProduct(RootUserId,
                    "testname", false);
            String productId2 = AddAgentProductCallService.AddAgentProduct(addAgentProductBean2);
            ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(RootUserId, productId2);
            String catalogId2 = productDetailsDto2.getCatalogList().get(0).getCatalogId();
            DetailsCatalogDto ca2 = productDetailsDto2.getCatalogList().stream().filter(x->x.getCatalogId().equals(catalogId2)).findFirst().get();
			int real2=TestDataManager.getRandomIntNum(2)+1;
			int virtual2=TestDataManager.getRandomIntNum(2)+1;
			
//            OpenAdjustStockBean.setUserId(RootUserId);
			String bizno=YMTDateUtil.getBeforeOrNextDay(0, "yyyyMMddHHmmss")+TestDataManager.getRandomNum(4);
            OpenAdjustStockBean.setBizNo(bizno);
            List<AdjustSkuStock> astocks=new ArrayList<AdjustSkuStock>();
            AdjustSkuStock adjustSkuStock1=new AdjustSkuStock();
//            adjustSkuStock1.setProduct_id(productId);
            adjustSkuStock1.setSku_id(catalogId);
            adjustSkuStock1.setStock_num(real);
            adjustSkuStock1.setVirtual_stock_num(virtual);
			astocks.add(adjustSkuStock1);
			
			 AdjustSkuStock adjustSkuStock2=new AdjustSkuStock();
//			 adjustSkuStock2.setProduct_id(productId);
			 adjustSkuStock2.setSku_id(catalogId2);
			 adjustSkuStock2.setStock_num(real2);
			 adjustSkuStock2.setVirtual_stock_num(virtual2);
			astocks.add(adjustSkuStock2);
				
			OpenAdjustStockBean.setSku_stocks(astocks);
			
			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper
					.selectApplicationBySellerId((long) RootUserId).get(0);
			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();
			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);
			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(OpenAdjustStockBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.sku.stock.adjust",
							app_id);
			OpenAdjustStockCall.setUrl(url);
			OpenAdjustStockCall.setData(openapiReq);
			OpenAdjustStockCall.callService();			
			Logger.verifyEquals("0000", OpenAdjustStockCall.getString("code"), "验证返回");
			Thread.sleep(5000);
			ProductDetailsDto productDetailsDto1 = AddAgentProductCallService.selectProductDetails(RootUserId, productId);
			DetailsCatalogDto rca1 = productDetailsDto1.getCatalogList().stream().filter(x->x.getCatalogId().equals(catalogId)).findFirst().get();
			Logger.verifyEquals(ca1.getRealStock()+real,rca1.getRealStock(),"验证real");
			Logger.verifyEquals(ca1.getVirtualStock()+virtual,rca1.getVirtualStock(),"验证virtual");
			Logger.verifyEquals(ca1.getStock()+real+virtual,rca1.getStock(),"验证stock");
			
			ProductDetailsDto rproductDetailsDto2 = AddAgentProductCallService.selectProductDetails(RootUserId, productId2);
			DetailsCatalogDto rca2 = rproductDetailsDto2.getCatalogList().stream().filter(x->x.getCatalogId().equals(catalogId2)).findFirst().get();
			Logger.verifyEquals(ca2.getRealStock()+real2,rca2.getRealStock(),"验证real");
			Logger.verifyEquals(ca2.getVirtualStock()+virtual2,rca2.getVirtualStock(),"验证virtual");
			Logger.verifyEquals(ca2.getStock()+real2+virtual2,rca2.getStock(),"验证stock");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
//	com.shop2cn.iapi.prodstock.testcase.Ts_OpenAdjustStock.Tc_OpenAdjustStock_006()
	@Test
	@Category({P1.class})
	@TestCase
	//com.shop2cn.iapi.agentproducts.testcase.Ts_UpdateProductBase
	public void Tc_OpenAdjustStock_003() {
		Logger.start(true,"更新商品库存信息-outsku更新");
		try {
			//前置数据
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(RootUserId,
                    "testname", false);
            String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
            ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(RootUserId, productId);
            String sku1 = productDetailsDto.getCatalogList().get(0).getSku();
            DetailsCatalogDto ca1 = productDetailsDto.getCatalogList().stream().filter(x->x.getSku().equals(sku1)).findFirst().get();
			int real=TestDataManager.getRandomIntNum(2)+1;
			int virtual=TestDataManager.getRandomIntNum(2)+1;
			
			AddAgentProductBean addAgentProductBean2 = AddAgentProductCallService.initAddAgentProduct(RootUserId,
                    "testname", false);
            String productId2 = AddAgentProductCallService.AddAgentProduct(addAgentProductBean2);
            ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(RootUserId, productId2);
            String sku2 = productDetailsDto2.getCatalogList().get(0).getSku();
            DetailsCatalogDto ca2 = productDetailsDto2.getCatalogList().stream().filter(x->x.getSku().equals(sku2)).findFirst().get();
			int real2=TestDataManager.getRandomIntNum(2)+1;
			int virtual2=TestDataManager.getRandomIntNum(2)+1;
			
//            OpenAdjustStockBean.setUserId(RootUserId);
			String bizno=YMTDateUtil.getBeforeOrNextDay(0, "yyyyMMddHHmmss")+TestDataManager.getRandomNum(4);
            OpenAdjustStockBean.setBizNo(bizno);
            List<AdjustSkuStock> astocks=new ArrayList<AdjustSkuStock>();
            AdjustSkuStock adjustSkuStock1=new AdjustSkuStock();
//            adjustSkuStock1.setProduct_id(productId);
            adjustSkuStock1.setOuter_sku_id(sku1);
            adjustSkuStock1.setStock_num(real);
            adjustSkuStock1.setVirtual_stock_num(virtual);
			astocks.add(adjustSkuStock1);
			
			 AdjustSkuStock adjustSkuStock2=new AdjustSkuStock();
//			 adjustSkuStock2.setProduct_id(productId);
			 adjustSkuStock2.setOuter_sku_id(sku2);
			 adjustSkuStock2.setStock_num(real2);
			 adjustSkuStock2.setVirtual_stock_num(virtual2);
			astocks.add(adjustSkuStock2);
				
			OpenAdjustStockBean.setSku_stocks(astocks);
			
			OpenApiWapper openApiWapper = new OpenApiWapper();
			Application application = openApiWapper
					.selectApplicationBySellerId((long) RootUserId).get(0);
			String app_id = application.getApp_id();
			String app_secret = application.getApp_secret();
			AuthCode authCode = openApiWapper.selectAuthCodeByAppId(app_id)
					.get(0);
			openapiReq.setApp_id(app_id);
			openapiReq.setSign_method("MD5");
			openapiReq.setAuth_code(authCode.getAuth_code());
			openapiReq.setTimestamp(YMTDateUtil.getSysTime());
			openapiReq.setNonce_str(TestDataManager.getRandomNum(31));
			openapiReq.setBiz_content(new FormatUtil()
					.beanToGSONString(OpenAdjustStockBean));
			openapiReq.setSign(OpenapiService.generateMD5(openapiReq,
					AesUtil.decrypt(app_secret)));
			String url = String
					.format("http://apigateway.ymatou.com/apigateway/v1?app_id=%s&method=sq.sku.stock.adjust",
							app_id);
			OpenAdjustStockCall.setUrl(url);
			OpenAdjustStockCall.setData(openapiReq);
			OpenAdjustStockCall.callService();			
			Logger.verifyEquals("0000", OpenAdjustStockCall.getString("code"), "验证返回");
			Thread.sleep(5000);
			ProductDetailsDto productDetailsDto1 = AddAgentProductCallService.selectProductDetails(RootUserId, productId);
			DetailsCatalogDto rca1 = productDetailsDto1.getCatalogList().stream().filter(x->x.getSku().equals(sku1)).findFirst().get();
			Logger.verifyEquals(ca1.getRealStock()+real,rca1.getRealStock(),"验证real");
			Logger.verifyEquals(ca1.getVirtualStock()+virtual,rca1.getVirtualStock(),"验证virtual");
			Logger.verifyEquals(ca1.getStock()+real+virtual,rca1.getStock(),"验证stock");
			
			ProductDetailsDto rproductDetailsDto2 = AddAgentProductCallService.selectProductDetails(RootUserId, productId2);
			DetailsCatalogDto rca2 = rproductDetailsDto2.getCatalogList().stream().filter(x->x.getSku().equals(sku2)).findFirst().get();
			Logger.verifyEquals(ca2.getRealStock()+real2,rca2.getRealStock(),"验证real");
			Logger.verifyEquals(ca2.getVirtualStock()+virtual2,rca2.getVirtualStock(),"验证virtual");
			Logger.verifyEquals(ca2.getStock()+real2+virtual2,rca2.getStock(),"验证stock");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}